package com.iterator.controller.web.systemManager;



import com.iterator.bean.web.pojo.BaseRespVo;
import com.iterator.bean.web.wy.admin.*;
import com.iterator.service.web.systemManager.AdminListService;
import com.iterator.util.StringTrim;
import com.iterator.util.UserNameAndPasswordValidatorUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: Silence_W
 * @date: 2021-01-10 14:55
 *
 * 系统管理
 *   管理员管理
 **/
@RestController
@RequestMapping("admin/admin")
public class AdminListController {
    @Autowired
    AdminListService adminListService;

    /**
     * 页面数据显示
     * @param page 页码
     * @param limit 页面限制
     * @param username 模糊查询
     * @param sort  排序类型
     * @param order 排序方式
     * @return
     */
    @RequestMapping("list")
    @RequiresPermissions("admin:admin:list")
    public BaseRespVo list(Integer page, Integer limit , String username, String sort, String order){
        String trim = StringTrim.trim(username);
        AdminVO issueVO =adminListService.queryAdmin(page,limit,trim,sort,order);
        return BaseRespVo.ok(issueVO);
    }
    //增加新的管理员数据
    @RequestMapping("create")
    @RequiresPermissions("admin:admin:create")
    public BaseRespVo list(@RequestBody CreateBO createBO, HttpServletRequest request){

        String s = UserNameAndPasswordValidatorUtil.userNameAndPasswordValidateUtil(createBO.getUsername(), createBO.getPassword());
        if (s!=null) {
            return BaseRespVo.fail(s);
        }
        int code =  adminListService.authen(createBO.getUsername());
         if (code==605||code==604){
             return BaseRespVo.fail("账号已存在");

         } else {
             CreateVO createVO = adminListService.insertAdmin(createBO, request);
             return BaseRespVo.ok(createVO);
         }
    }
    //删除问管理员数据
    @RequestMapping("delete")
    @RequiresPermissions("admin:admin:delete")
    public BaseRespVo delete(@RequestBody DeleteBO deleteBO){
     adminListService.delete(deleteBO.getId());
        return BaseRespVo.ok();
    }

    //编辑管理员数据
    @RequestMapping("update")
    @RequiresPermissions("admin:admin:update")
    public BaseRespVo update(@RequestBody UpdateBO updateBO){
        String s = UserNameAndPasswordValidatorUtil.userNameAndPasswordValidateUtil(updateBO.getUsername(), updateBO.getPassword());
        if (s!=null) {
            return BaseRespVo.fail(s);
        }
        int code =  adminListService.authen(updateBO.getUsername());
        if (code==605){
            return BaseRespVo.fail("账号已存在");


        }else {
            UpdateVO updateVO = adminListService.updateAdmin(updateBO);

            return BaseRespVo.ok(updateVO);
        }
    }
}
